﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using Windows.ApplicationModel.Core;
using Windows.Storage;
using Windows.UI.Xaml.Navigation;
using Windows.Web.Http;
using douban.Core.Apis;
using douban.Core.Repositories;
using douban.Core.Utils;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using douban.app.Views;

namespace douban.Core.ViewModels
{
    public class LoginVm:VmBase
    {
        private string _userName;
        private string _password;
        private readonly ConfigRepository _configRepository;

        public string UserName
        {
            get { return _userName; }
            set
            {
                if (value == _userName) return;
                _userName = value;
                OnPropertyChanged();
            }
        }

        public string Password
        {
            get { return _password; }
            set
            {
                if (value == _password) return;
                _password = value;
                OnPropertyChanged();
            }
        }

        public ICommand LoginCommand { get; private set; }

        public LoginVm(Repositories.ConfigRepository configRepository)
        {
            UserName = "rwecho@126.com";
            Password = "rw870415";
            LoginCommand = new RelayCommand(Login);
            _configRepository = configRepository;
          
          
        }

        private async void Login()
        {
            var authenticate = await Api.AuthGetToken(UserName, Password);

            if (authenticate != null)
            {
                authenticate.SetCommon();
                _configRepository.SetAuthenticate(authenticate);

                AppFrame.GoBack();
            }
        }

        public override void OnNavigatedTo(NavigationEventArgs e)
        {
            
        }

        public override void OnNavigatedFrom(NavigationEventArgs e)
        {
            
        }
    }
}
